package spring.proxy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class Chain {

    List<Advice> advices = new ArrayList<>();
    int index = -1;

    public Chain(List<Advice> advices, Method method, Object[] args, Object target) {
        this.advices = advices;
        this.method = method;
        this.args = args;
        this.target = target;
    }

    Method method;  // 目标方法
    Object[] args;  // 参数
    Object target; // 目标对象



    public Object proceed() throws InvocationTargetException, IllegalAccessException {
        if (index == advices.size()-1){
            return null;
        }

        // 调用目标方法
        method.invoke(target, args);

        return advices.get(++index).execute(this);
    }
}
